import { saveSearch, clearSearch, deleteSearch, loadSearch, loadArea, saveAreaMap } from '@/utils/cache'

const searchHistory = {
  state: {
    searchHistory: loadSearch(),
    areaMap: loadArea()
  },
  mutations: {
    SEARCH_HISTORY: (state, history) => {
      state.searchHistory = history
    },
    SET_AREA_MAP: (state, areaMap) => {
      state.areaMap = areaMap
    }
  },
  actions: {
    saveAreaMap({ commit }, query) {
      commit('SET_AREA_MAP', saveAreaMap(query))
    },
    saveSearchHistory({ commit }, query) {
      commit('SEARCH_HISTORY', saveSearch(query))
    },
    deleteSearchHistory({ commit }, query) {
      commit('SEARCH_HISTORY', deleteSearch(query))
    },
    clearSearchHistory({ commit }) {
      commit('SEARCH_HISTORY', clearSearch())
    }
  }
}

export default searchHistory
